﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class ChoseHeroItem : MonoBehaviour
    {
        public UILabel heroName;
        public UILabel heroLevel;
        public UILabel heroExp;
        public GameObject heroIconRoot;
        public GameObject heroExpRoot;
        public GameObject checkBox;
        public GameObject buttonGo;
        public UILabel worldName;
        public delegate void OnGoBattle(string guid);
        public OnGoBattle m_callback;

        public delegate void OnOpenHero(string guid);
        public OnGoBattle open_callback;

        public delegate void OnCheckBoxChange(string guid,bool value);
        public OnCheckBoxChange m_OnCheckBox;

        public string heroGuid;

        #region 按钮
        public GameObject goBattleBtn;
        public GameObject helpBattleBtn;
        #endregion
        
        public void SetHero(string guid,bool forSelect = false,bool isHelp=false)
        {

            heroGuid = guid;

            UIHero hero = StaticData.playerData.InstPlayerData.GetUIHero(guid);

            heroIconRoot.transform.DestroyChildren();
            GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/HeroIcon") as GameObject;
            GameObject icon = Instantiate(obj);
            icon.transform.parent = heroIconRoot.transform;
            icon.transform.localPosition = new Vector3(0, 0, 0);
            icon.transform.localScale = new Vector3(1, 1, 1);
            icon.GetComponent<HeroIcon>().SetHero(guid);
            Destroy(icon.GetComponent<BoxCollider>());

            if(!forSelect && hero.talentLevel > 0)
            {
                icon.GetComponent<HeroIcon>().UpdateRefine("+" + hero.talentLevel.ToString());
            }

            heroLevel.text = TableTextBlo.GetText("level") + hero.level.ToString();

            heroName.text = hero.heroName;

            heroExpRoot.SetActive(forSelect);
            checkBox.SetActive(forSelect);
            buttonGo.SetActive(!forSelect && !isHelp);

            if(forSelect)
            {
                long exp = hero.GetHeroProvideExp();
                heroExp.text = exp.ToString();
            }
            
            goBattleBtn.SetActive(!isHelp && !forSelect);
            helpBattleBtn.SetActive(isHelp && !forSelect);
        }
        /// <summary>
        /// 图鉴
        /// </summary>
        /// <param name="id"></param>
        /// <param name="forSelect"></param>
        /// <param name="isHelp"></param>
        //public void SetHeroInfo(string id, bool forSelect = false, bool isHelp = false)
        //{
        //    heroGuid = id;
        //    Dict.DataModel.TableHero hero = StaticData.playerData.InstPlayerData.GetUIHeroById(id);
        //    heroIconRoot.transform.DestroyChildren();
        //    GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/HeroIcon") as GameObject;
        //    GameObject icon = Instantiate(obj);
        //    icon.transform.parent = heroIconRoot.transform;
        //    icon.transform.localPosition = Vector3.zero;
        //    icon.transform.localScale = Vector3.one;
        //    icon.AddComponent<UIDragScrollView>();
        //    var script = icon.GetComponent<HeroIcon>();
        //    script.SetHeroWithId(id);
        //    var widget = icon.GetComponent<UIWidget>();
        //    icon.AddComponent<BoxCollider>().size = new Vector3(widget.width,widget.height,0);
        //    icon.AddComponent<UIButton>().onClick.Add(new EventDelegate(OnClickIcon)) ;
        //    worldName.text = GetHeroSpaceName(hero.space);
        //    Destroy(icon.GetComponent<BoxCollider>());
        //    heroName.text = TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(id));
        //}

        public void SetTreasure(string guid)
        {
            heroGuid = guid;

            UITreasure treasure = StaticData.playerData.InstPlayerData.GetUITreasure(guid);

            heroIconRoot.transform.DestroyChildren();
            GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/EquipIcon") as GameObject;
            GameObject icon = Instantiate(obj);
            icon.transform.parent = heroIconRoot.transform;
            icon.transform.localPosition = new Vector3(0, 0, 0);
            icon.transform.localScale = new Vector3(1, 1, 1);
            icon.GetComponent<EquipIcon>().SetTreasure(treasure);
            Destroy(icon.GetComponent<BoxCollider>());

            heroLevel.text = TableTextBlo.GetText("level") + treasure.strengthLevel.ToString();

            heroName.text = TableTextBlo.GetText("name_" + treasure.treasureId);

            heroExpRoot.SetActive(true);
            checkBox.SetActive(true);
            buttonGo.SetActive(false);
            helpBattleBtn.SetActive(false);

            long exp = treasure.currentExp + treasure.treasureData.exp;
            heroExp.text = exp.ToString();
            
        }

        // 上阵 获取
        public void OnGoBattleBtn()
        {
            if(m_callback != null)
            {
                m_callback(heroGuid);
            }
        }

        //助阵
        public void OnGoHelpBtn()
        {
            if(m_callback != null)
            {
                m_callback(heroGuid);
            }
        }

        //打开图集
        public void OnOpenHeroInfo()
        {
            if (open_callback != null)
            {
                open_callback(heroGuid);
            }
        }

        public void OnCheckBox()
        {
            if(m_OnCheckBox != null)
            {
                m_OnCheckBox(heroGuid,checkBox.GetComponent<UIToggle>().value);
            }
        }

        string GetHeroSpaceName(int space)
        {
            switch (space)
            {
                case 1:
                    return "SAO";
                case 2:
                    return "ALO";
                case 3:
                    return "GGO";
            }

            return "-";
        }

        void OnClickIcon()
        {
            PageManager.Instance.OpenPage("UIHeroPage", "heroId=" + heroGuid + "&showSwitchHero=false");
        }
    }

}
